import user from '../../utils/user'
import { openSalaryCard } from '../../utils/salary-card'
import { paid } from '../../utils/dictionaryKey'
import {
    getPaid
} from '../../api/get-paid.js'
Page({
    data: {
        color:'#04CD5A',
        type:'success',
        title: '',
        content: '',
        signVoucher:'',
        phoneList:[],
        isOpenSalaryCard: false,
        companyId: '',
        orderId:'',
        payId:'',
        intervalId:''
    },
    toservice(){
        wx.navigateTo({
          url: '/pages/service/service',
        })
    },
    // title=提现失败&content=实名认证信息与微信绑定信息不一致，请注销后用本人微信重新注册&type=warn&color=#FF7E68&id=605
    onReady() {
        
    },
    async onLoad(option) {
        console.log(option,'option')
        await user.waitLogin()
        console.log(paid, 'paid')
        this.setData({
            orderId: option.order,
            payId:option.payid,
            color:option.content.includes('转账处理中') ? '#FFB12D' : option.color,
            type: option.content.includes('转账处理中') ? 'waiting' : option.type,
            title: option.content.includes('转账处理中') ? '转账中' : option.title,
            content: paid[option.content] || option.content,
            signVoucher:option.signVoucher,
            isOpenSalaryCard: user.isOpenSalaryCard,
            companyId: option.id
        })
        console.log(this.data.isOpenSalaryCard, 'isOpenSalaryCard')
        const eventChannel = this.getOpenerEventChannel()
        console.log(eventChannel)
        eventChannel.on('phone',(data) => {
            console.log(data)
            this.setData({
                phoneList: data
            })
        })
        if(option.content.includes('转账处理中')){
            this.data.intervalId = setInterval(async ()=>{
                try{
                    const result = await getPaid({
                        id: this.data.payId,
                        detailOrderNo: this.data.orderId,
                        transferScene: ''
                    })
                    console.log(result, 'resultresult')
                    if(result && result.type === '转账失败'){
                        console.log('进入啦失败')
                        clearInterval(this.data.intervalId)
                        this.setData({
                            orderId: '',
                            payId: '',
                            color: '#FF7E68',
                            type: 'warn' ,
                            title: '提现失败',
                            content: paid[result.msg] || result.msg
                        })
                        return 0
                    } else if(result && result.msg){
                        clearInterval(this.data.intervalId)
                        this.setData({
                            orderId: '',
                            payId: '',
                            color:result.msg.includes('转账处理中') ? '#FFB12D' : '#04CD5A',
                            type: result.msg.includes('转账处理中') ? 'waiting' : 'success' ,
                            title: result.msg.includes('转账处理中') ? '转账中' : '提现成功',
                            content: paid[result.msg] || result.msg
                        })
                        return
                    }
                } 
                catch(err){
                    clearInterval(this.data.intervalId)
                    this.setData({
                        orderId: '',
                        payId: '',
                        color: '#FF7E68',
                        type: 'warn' ,
                        title: '提现失败',
                        content: paid[err.msg] || err.msg
                    })
                }
                console.log(result, 'resultresult')
            },5000)
        }
        
    },
    backHome(){
        wx.switchTab({
            url: '/pages/index/index'
        })	
    },
    async onPhone(){
        const linkmanList = this.data.phoneList
        const sheet = linkmanList.map(item => {
            return item.linkman + ' ' + item.phone
        })
        const result = await wx.showActionSheet({
            itemList: sheet,
        })
        wx.makePhoneCall({
            phoneNumber: linkmanList[result.tapIndex].phone
        })
    },
    onOpenSalaryCard(){
        openSalaryCard(this.data.companyId)
    },
    onUnload(){
        clearInterval(this.data.intervalId)
    }
})
